package com.example.demo.exception;

import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.JwtException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author:shixudong
 * @Date:Created in 9:19 2022/3/28
 */
@RestControllerAdvice
@Slf4j
@Component
public class GlobalExceptionHandler {

    @ExceptionHandler(AccessDeniedException.class)
    public String handleAuthorizationException(AccessDeniedException e) {
        log.error(e.getMessage());
        return  "没有权限，请联系管理员授权";
    }

    @ExceptionHandler(AccountExpiredException.class)
    public String handleAccountExpiredException(AccountExpiredException e) {
        log.error(e.getMessage(), e);
        return "过期";
    }

    @ExceptionHandler(ExpiredJwtException.class)
    @ResponseStatus(HttpStatus.OK)
    public String handle(ExpiredJwtException e) {
        log.error(e.getMessage(), e);
        return "jwt错误";
    }
}
